package com.demo.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo.dao.YiShengMapper;
import com.demo.dao.YiShengPaiBanMapper;
import com.demo.entity.Tuijian;
import com.demo.entity.YiSheng;
import com.demo.entity.YiShengPaiBan;

import net.sf.json.JSONObject;

@Service
public class TuiJianDoctorServie {
	@Autowired
	private YiShengMapper yiShengMapper;

	@Autowired
	private YiShengPaiBanMapper yiShengPaiBanMapper;

	public int deleteByPrimaryKey(Integer id) {
		return yiShengMapper.deleteByPrimaryKey(id);
	}

	public int insert(YiSheng record) {
		return yiShengMapper.insert(record);
	}

	public YiSheng selectByPrimaryKey(Integer id) {
		return yiShengMapper.selectByPrimaryKey(id);
	}

	public List<YiSheng> selectAll() {
		return yiShengMapper.selectAll();
	}

	public int updateByPrimaryKey(YiSheng record) {
		return yiShengMapper.updateByPrimaryKey(record);
	}

	public List<YiSheng> selectRandomNews() {
		return yiShengMapper.selectAll();
	}

	public List<YiSheng> findyishengBykeshi(String keshi) {
		return yiShengMapper.findyishengBykeshi(keshi);
	}

	public int deleteByPrimaryKeyp(Integer id) {
		return yiShengPaiBanMapper.deleteByPrimaryKey(id);
	}

	public int insertp(YiShengPaiBan record) {
		return yiShengPaiBanMapper.insert(record);
	}

	public YiShengPaiBan selectByPrimaryKeyp(Integer id) {
		return yiShengPaiBanMapper.selectByPrimaryKey(id);
	}

	public List<YiShengPaiBan> selectAllp() {
		return yiShengPaiBanMapper.selectAll();
	}

	public int updateByPrimaryKeyp(YiShengPaiBan record) {
		return yiShengPaiBanMapper.updateByPrimaryKey(record);
	}

	public List<YiShengPaiBan> findYishengByShijian(String jiuzhengshijian) {
		return yiShengPaiBanMapper.findYishengByShijian(jiuzhengshijian);
	}

	public List<YiShengPaiBan> findYishengByYisheng(String yisheng) {
		return yiShengPaiBanMapper.findYishengByYisheng(yisheng);
	}

	public List<YiShengPaiBan> findYishengPaiban(String yisheng, String jiuzhengshijian) {
		return yiShengPaiBanMapper.findYishengPaiban(yisheng, jiuzhengshijian);
	}

	public JSONObject recommand(String yonghu,String miaoshu) {
		
		JSONObject object = new JSONObject();
		String resultflag = "";
		List<YiSheng> doctorlistList = new ArrayList<YiSheng>();
		List<YiSheng> lisyList = yiShengMapper.selectAll();
		List<String> list = new ArrayList<String>();
		String firString = yonghu+",";
		miaoshu = miaoshu.replace("、", "") 
				.replace("；", "") 
				.replace(";", "") 
				.replace(".", "") 
				.replace(",", "")
				.replace("。", "");
		char d1 [] = miaoshu.toCharArray();
		if(d1.length>0) {
			for(int i=0;i<d1.length;i++) {
				firString+=(d1[i]+",");
			}
			list.add(firString);
			if(lisyList.size()>0) {
				for (YiSheng yiSheng : lisyList) {
					String resultString = yiSheng.getId()+",";
					if(yiSheng.getShanchang().length()>0) {
						String aString = yiSheng.getShanchang().replace("、", "") 
								.replace("；", "") 
								.replace(";", "") 
								.replace(".", "") 
								.replace(",", "")
								.replace("。", "");
						if(aString.length()>0) {
							char d [] = aString.toCharArray();
							if(d.length>0) {
								for(int i=0;i<d.length;i++) {
									resultString+=(d[i]+",");
								}
							}
						}
					}
					list.add(resultString);
				}
			}
			if(list.size()>0) {
				List<Tuijian> list2 = toCaculater(list, yonghu);
				List<Tuijian> list3 = list2.stream().sorted(Comparator.comparing(Tuijian ::getRate).reversed()).collect(Collectors.toList());
				 if(list3.size()>0) { for (Tuijian tuijian : list3) {
				 System.out.println("计算结果"+tuijian.toString()); } }
				 if(list3.size()==0) {
					 resultflag =  "NO_RESULT";  
				 }else {
					 int maxlength = (list3.size()<3)?list.size():3;
					 for(int i=0;i<maxlength;i++) {
						 if(list3.get(i).getYisheng()!=""&&list3.get(i).getYisheng()!=null) {
							 int id = Integer.valueOf(list3.get(i).getYisheng());
							 System.out.println("id"+id);
							 YiSheng yiSheng = yiShengMapper.selectByPrimaryKey(id);
							 doctorlistList.add(yiSheng);
						 }
					 }
				 }
				
			}
		}else {
			resultflag =  "ERROR_INPUT";  
		}
		if(lisyList.size()>0) {
			resultflag =  "SUCCESS";  
		}
		System.out.println(doctorlistList.size());
		object.put("flag", resultflag);
		object.put("doctorlist",doctorlistList);
		return object;

	}

	public static ArrayList<Tuijian> toCaculater(List<String> asArrayList, String olduserid) {
		System.out.println("input :");
		for (Object object : asArrayList) {
			System.out.println(object.toString());
		}
		ArrayList<String> Userlist = new ArrayList<String>();
		Map<String, Integer> userItemLength = new HashMap<String, Integer>();
		Map<String, Set<String>> itemUserCollection = new HashMap<String, Set<String>>();
		Set<String> items = new HashSet<String>();
		Map<String, Integer> userID = new HashMap<String, Integer>();
		Map<Integer, String> idUser = new HashMap<Integer, String>();
		int N = asArrayList.size();
		int[][] sparseMatrix = new int[N][N];
		for (int i = 0; i < N; i++) {
			String[] user_item = ((String) asArrayList.get(i)).split(",");
			int length = user_item.length;
			userItemLength.put(user_item[0], length - 1);
			userID.put(user_item[0], i);
			idUser.put(i, user_item[0]);
			Userlist.add(user_item[0]);
			itemUserCollection.remove(user_item[0]);
			for (int j = 1; j < length; j++) {
				if (items.contains(user_item[j])) {
					itemUserCollection.get(user_item[j]).add(user_item[0]);
				} else {
					items.add(user_item[j]);
					itemUserCollection.put(user_item[j], new HashSet<String>());
					itemUserCollection.get(user_item[j]).add(user_item[0]);
				}
			}
		}
		Set<Entry<String, Set<String>>> entrySet = itemUserCollection.entrySet();
		Iterator<Entry<String, Set<String>>> iterator = entrySet.iterator();
		while (iterator.hasNext()) {
			Set<String> commonUsers = iterator.next().getValue();
			for (String user_u : commonUsers) {
				for (String user_v : commonUsers) {
					if (user_u.equals(user_v)) {
						continue;
					}
					sparseMatrix[userID.get(user_u)][userID.get(user_v)] += 1;
				}
			}
		}
		ArrayList<Tuijian> listrecommand = new ArrayList<Tuijian>();
		Iterator<String> it1 = Userlist.iterator();
		System.out.println("result:");
		while (it1.hasNext()) {
			String user = it1.next().toString();
			int userid = userID.get(user);
			if (idUser.get(userid).equals(olduserid)) {
				for (int j = 0; j < sparseMatrix.length; j++) {
					if (j != userid) {
						double du = sparseMatrix[userid][j]
								/ Math.sqrt(userItemLength.get(idUser.get(userid)) * userItemLength.get(idUser.get(j)));
						Tuijian tuijian  = new Tuijian();
						tuijian.setYonghu(olduserid);
						tuijian.setYisheng(idUser.get(j));
						tuijian.setRate(mulDouble(du));
						listrecommand.add(tuijian);
					}
				}
			}

		}
		return listrecommand;
	}

	// double类型的数据进行处理
	public static double mulDouble(double f) {
		BigDecimal b = new BigDecimal(f);
		double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return f1;
	}
}
